﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data
{
    class DataBase
    {
        public static string Login_ID = "";
        public static string Login_Name = "";
        public static SqlConnection sqlConnection;
        public static string openConnstr = @"Data Source=114.115.145.87;Initial Catalog=数据;Persist Security Info=True;uid=sa;pwd=lszTOOR123456";
        public static SqlDataAdapter sqlAdapter; 
        public static DataSet dataSet;
        public static DataTable dataTable;
        DataTable distributorTable;

        public static SqlConnection getcon()
        {
            sqlConnection = new SqlConnection(openConnstr);
            sqlConnection.Open();
            return sqlConnection;
        }

        public void con_open()
        {
            getcon();
        }


        public void conn_close()
        {
            if (sqlConnection.State == ConnectionState.Open)
            {
                sqlConnection.Close();
                sqlConnection.Dispose();
            }
        }

        public SqlDataReader getsdr(string sqlstr)
        {
            getcon();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = sqlstr;
            SqlDataReader My_Reader = sqlCommand.ExecuteReader();
            return My_Reader;
        }

        public int dosqlcom(string sqlstr)
        {
            getcon();
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlConnection);
            int number=sqlcom.ExecuteNonQuery();
            sqlcom.Dispose();
            conn_close();
            return number;
        }

        public DataTable show(string sqlstr) {

            getcon();
            SqlCommand cmd = new SqlCommand(sqlstr, sqlConnection);
            distributorTable = new DataTable();
            sqlAdapter = new SqlDataAdapter(cmd);
            sqlAdapter.Fill(distributorTable);
            return distributorTable;
        }

        public DataTable Distributortableinsert( string sqlstr) {
            getcon();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = sqlstr;
            sqlCommand.ExecuteNonQuery();
            return distributorTable;
        }

        public DataTable Distributortabledelete(string sqlstr) {

            getcon();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = sqlstr;
            sqlCommand.ExecuteNonQuery();
            return distributorTable;
        }



        public DataSet getDs(string sqlstr, string tableName)
        {
            getcon();
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, sqlConnection);
            DataSet My_DataSet = new DataSet();
            sqlda.Fill(My_DataSet, tableName);
            conn_close();
            return My_DataSet;
        }
        public DataTable get_areacode_from_Areacode(string sqlstr) {
            getcon();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand = new SqlCommand(sqlstr);
            sqlAdapter = new SqlDataAdapter(sqlCommand);
            dataSet = new DataSet();
            sqlAdapter.Fill(dataSet,"dbo.S_area");
            sqlAdapter.Fill(dataTable);
            dataTable = dataSet.Tables["dbo.S_area"];
            return dataTable;
        }
    }
}
